package com.gxa.saas.portal.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.saas.core.vo.UserOderVo;
import com.gxa.saas.portal.service.SelectOrderService;
import com.gxa.saas.portal.thread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "c端查询订单模块")
@Controller
public class IndentController {
    @Autowired
    private SelectOrderService selectOrderService;

    @Autowired
    private UserThreadLocal userThreadLocal;

    @ApiOperation("查询全部订单")
    @ResponseBody
    @GetMapping("/GoodsIndent/selectIndent")
    public Map selectIndent() throws JsonProcessingException {
        String value = String.valueOf(userThreadLocal.get());
        ObjectMapper objectMapper = new ObjectMapper();
        com.gxa.saas.core.entity.vo.luoj.UserVO userVO = objectMapper.readValue(value, com.gxa.saas.core.entity.vo.luoj.UserVO.class);
        String userId = userVO.getId().toString();
        Map map = new HashMap();
        if (StringUtils.isEmpty(userId)){
            map.put("code",400);
            map.put("message","userId参数错误");
            return map;
        }
        List<UserOderVo> userOderVos = this.selectOrderService.selectUserOrder(userId);
        if (ObjectUtils.isEmpty(userOderVos)){
            map.put("code",300);
            map.put("message","无数据");
            return map;
        }
        map.put("code",200);
        map.put("data",userOderVos);
        return map;
    }

    @ApiOperation("按状态查询订单")
    @ResponseBody
    @GetMapping("/GoodsIndent/selectStatusIndent")
    public Map selectStatusIndent(@RequestParam("odStatus") Integer odStatus) throws JsonProcessingException {
        String value = String.valueOf(userThreadLocal.get());
        ObjectMapper objectMapper = new ObjectMapper();
        com.gxa.saas.core.entity.vo.luoj.UserVO userVO = objectMapper.readValue(value, com.gxa.saas.core.entity.vo.luoj.UserVO.class);
        String userId = userVO.getId().toString();
        Map map = new HashMap();
        if (StringUtils.isEmpty(userId)){
            map.put("code",400);
            map.put("message","userId参数错误");
            return map;
        }
        if (StringUtils.isEmpty(odStatus)){
            map.put("code",400);
            map.put("message","odStatus参数错误");
            return map;
        }
        List<UserOderVo> userOderVos = this.selectOrderService.selectStatusOrder(userId, odStatus);
        if (ObjectUtils.isEmpty(userOderVos)){
            map.put("code",300);
            map.put("message","无数据");
            return map;
        }
        map.put("code",200);
        map.put("data",userOderVos);
        return map;
    }

    @ApiOperation("订单详细信息")
    @ResponseBody
    @GetMapping("/GoodsIndent/detailsIndent")
    public Map detailsIndent(@RequestParam("odNumber") String odNumber) throws JsonProcessingException {
        String value = String.valueOf(userThreadLocal.get());
        ObjectMapper objectMapper = new ObjectMapper();
        com.gxa.saas.core.entity.vo.luoj.UserVO userVO = objectMapper.readValue(value, com.gxa.saas.core.entity.vo.luoj.UserVO.class);
        String userId = userVO.getId().toString();
        Map map = new HashMap();
        if (StringUtils.isEmpty(odNumber)){
            map.put("code",400);
            map.put("message","userId参数错误");
            return map;
        }
        List<UserOderVo> userOderVos = this.selectOrderService.selectDetailsOrder(odNumber);
        if (ObjectUtils.isEmpty(userOderVos)){
            map.put("code",300);
            map.put("message","无数据");
            return map;
        }
        map.put("code",200);
        map.put("data",userOderVos);
        return map;
    }

}
